package com.dream.root.common.context;

import java.util.HashMap;
import java.util.Map;

/**
 * BaseContextHandler Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 27/07/2018
 * Time: 21:41
 * Description: BaseContextHandler
 */
public class BaseContextHandler {
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    private static String getObjectValue(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    /**
     * 清空 threadLocal
     */
    public static void remove() {
        if (null != threadLocal)
            threadLocal.remove();
    }
}

